#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import unittest
from ExercisesAdvanced import HTMLTestReport
from functools import wraps


def Screen(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            func_result = func(*args, **kwargs)
            print('@%s passed!' % str(func.__name__), end='\n')
        except Exception as e:
            print('@%s failed: %s' % (str(func.__name__), str(e)), end='\n')
            # 这里需要抛出异常，否则unittest检测不到异常，会认为case执行通过
            raise e
        return func_result
    return wrapper


class TestFixtures(unittest.TestCase):

    def assertration(self, assert_type, *args):
        assert_dict = {
            "assertTrue": self.assertTrue,
            "assertFalse": self.assertFalse,
            "assertEqual": self.assertEqual
        }
        try:
            assert_result = assert_dict[assert_type](*args)
            return assert_result
        except Exception as e:
            # 这里需要抛出异常，否则unittest检测不到异常，会认为case执行通过
            raise e

    @classmethod
    def setUpClass(cls):
        print('******************** Start to test Class "%s" ********************' % str(cls.__name__), end='\n')

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        print('<-------- Start to test Case "%s" -------->' % str(self._testMethodName))

    def tearDown(self):
        pass


class TestCasesP0(TestFixtures):

    @Screen
    def testcase01(self):
        self.assertration("assertTrue", 1 == 1)

    @Screen
    def testcase02(self):
        self.assertration("assertTrue", "")


class TestCasesP1(TestFixtures):

    @Screen
    def testcase03(self):
        self.assertration("assertTrue", int('asd') == 1)

    @Screen
    def testcase04(self):
        self.assertration("assertTrue123", 1 == 1)

    @Screen
    def testcase05(self):
        self.assertration("assertTrue", 2 == 1)


if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(TestCasesP0("testcase01"))
    # suite.addTest(TestCasesP0("testcase02"))
    # suite.addTest(TestCasesP1("testcase03"))
    # suite.addTest(TestCasesP1("testcase04"))
    # suite.addTest(TestCasesP1("testcase05"))

    # fp = open(r'C:\Users\xiaoyao\Desktop\result.html', 'wb')
    # runner = HTMLTestReport.HTMLTestRunner(stream=fp, title='百度搜索测试报告', description='用例执行情况：')
    # runner.run(suite)
    # fp.close()

    runner = unittest.TextTestRunner()
    runner.run(suite)


